package com.example.demo;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.DeferredResult;

import com.example.demo.service.MsgService;


@Controller
@RequestMapping("/")
public class MsgControl {
	
	@Resource(name="msgService")
	private MsgService msgService;
	
	@ResponseBody
	@RequestMapping(value = "/getMsg", produces="text/event-stream;charset=UTF-8")
	DeferredResult<String> getMsg(HttpServletResponse response) throws IOException {
		response.setContentType("text/event-stream");
    	response.setCharacterEncoding("UTF-8");
    	response.setStatus(200);
    	msgService.removeErrorResponse();
    	msgService.getListRes().add(response);
    	if(!response.getWriter().checkError()){
    		response.getWriter().write("data:hello\n\n");
    		response.getWriter().flush();
    	}
    	DeferredResult<String> df = new DeferredResult<String>(60000l);
    	return df;
    }
}
